<?
 require_once("functions.php");
 
function AddPhone($new_phone_array)
{
	$database = getDB();
	
	$new_phone_array = arrayEscape($new_phone_array, $database);

	$statement = "INSERT INTO Phone (number, Contact_type_id, Customer_Name_id) VALUES ('$new_phone_array[newPhone]', '$new_phone_array[contact_type]', '$new_phone_array[id]')";

	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
}

function delPhone($phoneId)
{
	$database = getDB();
	

	$statement = "DELETE FROM Phone WHERE id = '$phoneId'";

	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
}

function getPhoneEntry($phoneId)
{
	$database = getDB();

	$statement = "SELECT Phone.id AS id, number, Contact_type_id, value AS Contact, Customer_Name_id AS Customer FROM Phone LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Phone.id = '$phoneId'";

	$results = $database->query($statement);
	
	$database->close();
	
	return $results;
}

function getUserPhones($Customer_id)
{
	$database = getDB();
	
	$statement = "SELECT Phone.id AS id, number, value AS Contact FROM Phone LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Customer_Name_id = '$Customer_id'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}


function updatePhone($update_email_array)
{
	$database = getDB();
	
	$update_email_array = arrayEscape($update_email_array, $database);

	$statement = "UPDATE Phone SET number = '$update_email_array[Phone]', Contact_type_id = '$update_email_array[contact_type_id]', Customer_Name_id = '$update_email_array[Customer_id]' WHERE id = '$update_email_array[id]'";
 
 	print($statement);
 
	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error, 'statement' => $statement);
}


?>